package org.kardigen.lg.admin.client.training;

import org.kardigen.lg.admin.client.training.event.ChapterSelectionEvent;
import org.kardigen.lg.admin.client.training.event.TrainingSelectionEvent;
import org.kardigen.lg.admin.client.training.event.WordSelectionEvent;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.ui.DeckPanel;

public class TrainingsEditorViewPresenter implements TrainingsEditorViewDisplay.Presenter {

	private final TrainingsEditorViewDisplay display;
	private final EventBus eventBus;

	public TrainingsEditorViewPresenter(TrainingsEditorViewDisplay display, final EventBus eventBus) {
		this.display = display;
		this.eventBus = eventBus;

		final DeckPanel contentPanel = display.getContentPanel();

		final TrainingEditorView trainingEditorView = new TrainingEditorView();
		contentPanel.add(trainingEditorView);
		final TrainingEditorViewPresenter trainingEditorViewPresenter = new TrainingEditorViewPresenter(
		      trainingEditorView);

		eventBus.addHandler(TrainingSelectionEvent.TYPE, new TrainingSelectionEvent.Handler() {

			public void onEvent(TrainingSelectionEvent trainingEvent) {
				contentPanel.showWidget(contentPanel.getWidgetIndex(trainingEditorView));
				trainingEditorViewPresenter.start(trainingEvent.getProxy(), eventBus);
			}
		});

		final ChapterEditorView chapterEditorView = new ChapterEditorView();
		contentPanel.add(chapterEditorView);
		final ChapterEditorViewPresenter chapterEditorViewPresenter = new ChapterEditorViewPresenter(chapterEditorView);

		eventBus.addHandler(ChapterSelectionEvent.TYPE, new ChapterSelectionEvent.Handler() {

			public void onEvent(ChapterSelectionEvent trainingEvent) {
				contentPanel.showWidget(contentPanel.getWidgetIndex(chapterEditorView));
				chapterEditorViewPresenter.start(trainingEvent.getParent(), trainingEvent.getProxy(), eventBus);
			}
		});

		final WordEditorView wordEditorView = new WordEditorView();
		contentPanel.add(wordEditorView);
		final WordEditorViewPresenter wordEditorViewPresenter = new WordEditorViewPresenter(wordEditorView);

		eventBus.addHandler(WordSelectionEvent.TYPE, new WordSelectionEvent.Handler() {

			public void onEvent(WordSelectionEvent wordSelectionEvent) {
				contentPanel.showWidget(contentPanel.getWidgetIndex(wordEditorView));
				wordEditorViewPresenter.start(wordSelectionEvent.getRoot(), wordSelectionEvent.getParent(),
				      wordSelectionEvent.getWord(), eventBus);
			}
		});

	}


}
